<?php

namespace App\Http\Controllers;

use App\Models\YzsEquipment;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\EquipmentRequest;

class EquipmentController extends CommonController
{
    /**
     *获取友情链接列表
     */
    public function getIndex()
    {
        $lists = YzsEquipment::orderBy('id','asc')->paginate(10);

        return view('equipment.index')->with('lists', $lists)->with("actions", $this->actions);
    }


    /**
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $sql = YzsEquipment::orderBy('id', 'asc');

        if (isset($data['title']) && $data['title'] != '') {
            $sql->where('title', 'like', '%' . trim($data['title']) . '%');
        }

        if (isset($data['e_ip']) && $data['e_ip'] != '') {
            $sql->where('equipment_ip', 'like', '%' . trim($data['e_ip']) . '%');
        }

        if (isset($data['c_ip']) && $data['c_ip'] != '') {
            $sql->where('client_ip', 'like', '%' . trim($data['c_ip']) . '%');
        }

        if (isset($data['status']) && $data['status'] != '') {
            $sql->where('status', $data['status']);
        }

        $lists = $sql->paginate(10);

        return view('equipment.search')->with("lists", $lists)->with("title", trim($data['title']))
            ->with("e_ip", trim($data['e_ip']))->with("c_ip", trim($data['c_ip']))->with("status", $data['status'])
            ->with("actions", $this->actions);
    }



    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        return view('equipment.create');
    }

    /**
     * 执行创建页面
     */
    public function postCreate(EquipmentRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        $data['status'] = 1;
        $result = YzsEquipment::create($data);

        if (is_object($result)) {
            return AjaxCallbackMessage('创建成功', true, url('equipment/index'));
        } else {
            return AjaxCallbackMessage('创建失败', false);
        }
    }


    /**
     *获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = YzsEquipment::where('id', $data['id'])->first();

        return view('equipment.edit')->with('list', $list);
    }

    /**
     * 执行页面编辑
     */
    public function postEdit(EquipmentRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType']);
        YzsEquipment::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('equipment/index'));
    }



    /**
     * 执行一个删除
     */
    public function postDelete(Request $request){
        $data = $request->input();
        YzsEquipment::where('id', $data['eid'])->delete();

        return AjaxCallbackMessage('删除成功', true, url('equipment/index'));
    }

}
